package top.tupobi.zing.service.socket;

import com.google.gson.Gson;

import java.util.Timer;
import java.util.TimerTask;

import top.tupobi.zing.network.HttpConfig;
import top.tupobi.zing.service.socket.listener.SocketResponseListener;
import top.tupobi.zing.service.socket.packet.DataType;
import top.tupobi.zing.service.socket.packet.HeartbeatModel;
import top.tupobi.zing.service.socket.packet.SocketPacket;
import top.tupobi.zing.utils.CPUThreadPool;

public class ProcessPacket {

    private ClientSocketProcessor mClientSocketProcessor;
    private SocketResponseListener mSocketResponseListener;

    public ProcessPacket(ClientSocketProcessor clientSocketProcessor) {
        mClientSocketProcessor = clientSocketProcessor;
    }

    public void setSocketResponseListener(SocketResponseListener socketResponseListener) {
        mSocketResponseListener = socketResponseListener;
    }

    public void parsePacket(String data) {
        SocketPacket packet = new Gson().fromJson(data, SocketPacket.class);
        if (DataType.CODE_SOCKET_STATUS_ABNORMAL == packet.getCode()) {
            System.out.println("接收到异常包状态，断开重连重新心跳");
            // 包状态异常，断开并重连，重新开始心跳
            mClientSocketProcessor.disConnectSocket();
            mClientSocketProcessor.connect(HttpConfig.SOCKET_IP, HttpConfig.SOCKET_PORT);

            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    postHeartbeat();
                }
            }, 2000);
        } else if (DataType.CODE_SOCKET_STATUS_NORMAL == packet.getCode()) {
            System.out.print("--包状态正常--");
            // 包状态正常，判断是心跳包还是其他包
            // 心跳包
            if (DataType.DATA_TYPE_NORMAL_HEARTBEAT == packet.getDataType()) {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        mClientSocketProcessor.sendData(new Gson().toJson(packet));
                    }
                }, 2000);

            } else if (DataType.DATA_TYPE_ADD_FRIEND == packet.getDataType()) {
                // 添加好友的回送信息，data中只有String
                if (mSocketResponseListener != null) {
                    mSocketResponseListener.onAddFriendResponse(packet.getData());
                }
            } else if ((DataType.DATA_TYPE_NEW_FRIEND_APPLY == packet.getDataType())) {
                // 新的好友申请回送信息，data中只有fromUsername
                if (mSocketResponseListener != null) {
                    mSocketResponseListener.onNewFriendApply(packet.getData());
                }
            } else if ((DataType.DATA_TYPE_MESSAGE_SEND_RESULT == packet.getDataType())) {
                // 发送消息结果
                if (mSocketResponseListener != null) {
                    mSocketResponseListener.onSendMessageResult(packet.getData());
                }
            } else if (DataType.DATA_TYPE_MESSAGE_RECEIVE == packet.getDataType()) {
                // 接收消息
                if (mSocketResponseListener != null) {
                    mSocketResponseListener.onReceiveMessage(packet.getData());
                }
            }
        }
    }

    public void postPacket(SocketPacket packet) {
        // 其他包单独开线程
        CPUThreadPool.getInstance().execute(() -> {
            if (!mClientSocketProcessor.isSocketConnected()) {
                mClientSocketProcessor.reConnect(HttpConfig.SOCKET_IP, HttpConfig.SOCKET_PORT);
            }
            System.out.println("发送给服务端的str == " + new Gson().toJson(packet));
            mClientSocketProcessor.sendData(new Gson().toJson(packet));
        });
    }

    public void postHeartbeat() {
        // 心跳包单独一个线程
        SocketPacket packet = SocketPacket.createPacket(DataType.DATA_TYPE_NORMAL_HEARTBEAT);
        packet.setMessage("heartbeatModel heartbeatModel");
        HeartbeatModel heartbeatModel = new HeartbeatModel();
        heartbeatModel.setHeartVoice(DataType.HEARTBEAT_TYPE_NORMAL);
        heartbeatModel.setHeartbeat(0);
        heartbeatModel.setExt1("dong ji");
        packet.setData(new Gson().toJson(heartbeatModel));
        if (!mClientSocketProcessor.isSocketConnected()) {
            mClientSocketProcessor.reConnect(HttpConfig.SOCKET_IP, HttpConfig.SOCKET_PORT);
        }
        System.out.println("发送给服务端的str == " + new Gson().toJson(packet));
        mClientSocketProcessor.sendData(new Gson().toJson(packet));
    }
}
